python读gz文件 |
您所在的位置:网站首页 › python 打开gz文件 › python读gz文件 |
1、Python2中指定编码读取gzip文件import gzip fp = gzip.open('foo.gz') contents = fp.read() #contents是foo.gz的未压缩字节 fp.close() u_str = contents.decode('utf-8') #u_str 现在是unicode 字符串 2、Python3中指定编码读取gzip文件import gzip gzip.open('file.gz', 'rt', encoding='utf-8') 3、Python2中gzip文件常用操作 1)读取压缩文件的示例代码:import gzip with gzip.open('file.txt.gz', 'rb') as f: file_content = f.read() 2)创建压缩gzip文件示例代码:import gzip content = "Lots of content here" with gzip.open('file.txt.gz', 'wb') as f: f.write(content) 3)gzip压缩现有文件的示例代码:import gzip import shutil with open('file.txt', 'rb') as f_in, gzip.open('file.txt.gz', 'wb') as f_out: shutil.copyfileobj(f_in, f_out) 4、Python3中gzip文件的常用操作 1)读取压缩文件的示例代码:import gzip with gzip.open('/home/joe/file.txt.gz', 'rb') as f: file_content = f.read() 2)创建压缩gzip文件的示例代码:import gzip content = b"Lots of content here" with gzip.open('/home/joe/file.txt.gz', 'wb') as f: f.write(content) 3)gzip压缩现有文件的示例代码:import gzip import shutil with open('/home/joe/file.txt', 'rb') as f_in: with gzip.open('/home/joe/file.txt.gz', 'wb') as f_out: shutil.copyfileobj(f_in, f_out) 4)gzip压缩二进制字符串的示例代码:import gzip s_in = b"Lots of content here" s_out = gzip.compress(s_in) |
今日新闻 |
点击排行 |
|
推荐新闻 |
图片新闻 |
|
专题文章 |
CopyRight 2018-2019 实验室设备网 版权所有 win10的实时保护怎么永久关闭 |